﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DcsGauges.Gui.Rendering;
using DcsGauges.Gui.Gauges.Interfaces;
using Microsoft.Xna.Framework.Graphics;
using DcsGauges.Shared.DataStructures;
using DcsGauges.DataStructures;
using DcsGauges.Gui.Gauges.Trigger;
using DcsGauges.Gui.Gauges.ValueConverters;
using Microsoft.Xna.Framework;

namespace DcsGauges.Gui.Gauges.Panels
{
    class LightsControlPanel : GuiRenderableBase, IMultiTriggerGauge
    {
        #region Fields & Properties

        private Texture2D                                                               backgroundTexture                       = null;
        private Dictionary<DcsProperty, Tuple<GuiRenderableBase, ITriggerGauge>>        subGauges                               = new Dictionary<DcsProperty,Tuple<GuiRenderableBase,ITriggerGauge>>();

        #endregion


        #region Constructors

        public LightsControlPanel(GuiRenderableBase parent, URect area) : base(parent, area)
        {
            this.backgroundTexture                          = TextureManager.GetTexture("Textures/Gauges/Backgrounds/LightControlPanel");

            ThreeStateToggleSwitchGauge flash               = new ThreeStateToggleSwitchGauge(this, new URect(new ULoc(0.12f, 0.1f), new USize(0.13f, 0.13f)), 
                DcsProperties.Panels.Right.LightControl.PositionFlash, 
                TextureManager.GetTexture("Textures/Gauges/Foregrounds/ToggleswitchDown"),
                TextureManager.GetTexture("Textures/Gauges/Foregrounds/ToggleswitchCenter"),
                TextureManager.GetTexture("Textures/Gauges/Foregrounds/ToggleswitchUp"),
                new ThreeStateToggleSwitchConverter());

            AnalogRotatorSwitchGauge formation              = new AnalogRotatorSwitchGauge(this, new URect(new ULoc(0.4f, 0.07f), new USize(0.2f, 0.2f)),
                DcsProperties.Panels.Right.LightControl.Formation,
                TextureManager.GetTexture("Textures/Gauges/Foregrounds/RotatorKnob"),
                new LinearConverter(new InOutMapping(0.0f, MathEx.Deg2Rad(225.0f)), new InOutMapping(1.0f, MathEx.Deg2Rad(500.0f))));

            TwoStateToggleSwitchGauge antiCollission        = new TwoStateToggleSwitchGauge(this, new URect(new ULoc(0.75f, 0.1f), new USize(0.13f, 0.13f)), 
                DcsProperties.Panels.Right.LightControl.AntiCollision,
                TextureManager.GetTexture("Textures/Gauges/Foregrounds/ToggleswitchDown"),
                TextureManager.GetTexture("Textures/Gauges/Foregrounds/ToggleswitchUp"),
                new TwoStateToggleSwitchConverter());
 
            AnalogRotatorSwitchGauge engineInstruments      = new AnalogRotatorSwitchGauge(this, new URect(new ULoc(0.215f, 0.32f), new USize(0.15f, 0.15f)),
                DcsProperties.Panels.Right.LightControl.EngineInstruments,
                TextureManager.GetTexture("Textures/Gauges/Foregrounds/RotatorKnob"),
                new LinearConverter(new InOutMapping(0.0f, MathEx.Deg2Rad(225.0f)), new InOutMapping(1.0f, MathEx.Deg2Rad(500.0f))));

            AnalogRotatorSwitchGauge auxInstruments         = new AnalogRotatorSwitchGauge(this, new URect(new ULoc(0.655f, 0.32f), new USize(0.15f, 0.15f)),
                DcsProperties.Panels.Right.LightControl.AuxiliaryInstruments,
                TextureManager.GetTexture("Textures/Gauges/Foregrounds/RotatorKnob"),
                new LinearConverter(new InOutMapping(0.0f, MathEx.Deg2Rad(220.0f)), new InOutMapping(1.0f, MathEx.Deg2Rad(505.0f))));

            AnalogRotatorSwitchGauge flightInstruments      = new AnalogRotatorSwitchGauge(this, new URect(new ULoc(0.41f, 0.53f), new USize(0.18f, 0.18f)),
                DcsProperties.Panels.Right.LightControl.FlightInstruments,
                TextureManager.GetTexture("Textures/Gauges/Foregrounds/RotatorKnob"),
                new LinearConverter(new InOutMapping(0.0f, MathEx.Deg2Rad(215.0f)), new InOutMapping(1.0f, MathEx.Deg2Rad(510.0f))));

            TwoStateToggleSwitchGauge signalLights          = new TwoStateToggleSwitchGauge(this, new URect(new ULoc(0.08f, 0.57f), new USize(0.13f, 0.13f)), 
                DcsProperties.Panels.Right.LightControl.SignalLights,
                TextureManager.GetTexture("Textures/Gauges/Foregrounds/ToggleswitchDown"),
                TextureManager.GetTexture("Textures/Gauges/Foregrounds/ToggleswitchUp"),
                new TwoStateToggleSwitchConverter());

            TwoStateToggleSwitchGauge accelAndComp          = new TwoStateToggleSwitchGauge(this, new URect(new ULoc(0.79f, 0.57f), new USize(0.13f, 0.13f)), 
                DcsProperties.Panels.Right.LightControl.AccelAndComp,
                TextureManager.GetTexture("Textures/Gauges/Foregrounds/ToggleswitchDown"),
                TextureManager.GetTexture("Textures/Gauges/Foregrounds/ToggleswitchUp"),
                new TwoStateToggleSwitchConverter());

            AnalogRotatorSwitchGauge flood                  = new AnalogRotatorSwitchGauge(this, new URect(new ULoc(0.215f, 0.76f), new USize(0.15f, 0.15f)),
                DcsProperties.Panels.Right.LightControl.Flood,
                TextureManager.GetTexture("Textures/Gauges/Foregrounds/RotatorKnob"),
                new LinearConverter(new InOutMapping(0.0f, MathEx.Deg2Rad(215.0f)), new InOutMapping(1.0f, MathEx.Deg2Rad(510.0f))));

            AnalogRotatorSwitchGauge console                = new AnalogRotatorSwitchGauge(this, new URect(new ULoc(0.655f, 0.76f), new USize(0.15f, 0.15f)),
                DcsProperties.Panels.Right.LightControl.Console,
                TextureManager.GetTexture("Textures/Gauges/Foregrounds/RotatorKnob"),
                new LinearConverter(new InOutMapping(0.0f, MathEx.Deg2Rad(215.0f)), new InOutMapping(1.0f, MathEx.Deg2Rad(510.0f))));

            this.subGauges.Add(flash.DataProperty,              new Tuple<GuiRenderableBase, ITriggerGauge>(flash,              flash));
            this.subGauges.Add(formation.DataProperty,          new Tuple<GuiRenderableBase, ITriggerGauge>(formation,          formation));
            this.subGauges.Add(antiCollission.DataProperty,     new Tuple<GuiRenderableBase, ITriggerGauge>(antiCollission,     antiCollission));
            this.subGauges.Add(engineInstruments.DataProperty,  new Tuple<GuiRenderableBase, ITriggerGauge>(engineInstruments,  engineInstruments));
            this.subGauges.Add(auxInstruments.DataProperty,     new Tuple<GuiRenderableBase, ITriggerGauge>(auxInstruments,     auxInstruments));
            this.subGauges.Add(flightInstruments.DataProperty,  new Tuple<GuiRenderableBase, ITriggerGauge>(flightInstruments,  flightInstruments));
            this.subGauges.Add(signalLights.DataProperty,       new Tuple<GuiRenderableBase, ITriggerGauge>(signalLights,       signalLights));
            this.subGauges.Add(accelAndComp.DataProperty,       new Tuple<GuiRenderableBase, ITriggerGauge>(accelAndComp,       accelAndComp));
            this.subGauges.Add(flood.DataProperty,              new Tuple<GuiRenderableBase, ITriggerGauge>(flood,              flood));
            this.subGauges.Add(console.DataProperty,            new Tuple<GuiRenderableBase, ITriggerGauge>(console,            console));
        }

        #endregion


        #region Overrides

        public override void Render(SpriteBatch spriteBatch, GraphicsDevice device, Rectangle parentRect, float globalZIndex)
        {
            Rectangle? origianlDim          = this.keepAspectRatio ? this.backgroundTexture.Bounds : new Rectangle?();
            Rectangle viewportRect          = this.CalcViewportRect(parentRect, origianlDim);
            
            spriteBatch.Draw(this.backgroundTexture, viewportRect, null, Color.White, 0.0f, Vector2.Zero, SpriteEffects.None, base.NextFrontIndex(globalZIndex, 1));

            foreach (var sg in this.subGauges)
            {
                sg.Value.Item1.Render(spriteBatch, device, viewportRect, base.NextFrontIndex(globalZIndex, 2));
            }

        }

        public override void InjectMouseEvent(Input.Events.MouseEvent evt, GameTime gt)
        {
            foreach (var sg in this.subGauges)
            {
                sg.Value.Item1.InjectMouseEvent(evt, gt);
            }
        }

        #endregion


        #region Interface Implementation

        #region Events

        public event EventHandler<TriggerValueEventArgs>  TriggerEvent              = delegate { };

        #endregion

        #region Properties

        public Shared.DataStructures.DcsProperty[] Properties
        {
            get { throw new NotImplementedException(); }
        }

        #endregion

        #region Operations

        public void SetValue(Shared.DataStructures.DcsProperty property, float value)
        {
            throw new NotImplementedException();
        }

        public float GetValue(Shared.DataStructures.DcsProperty property)
        {
            throw new NotImplementedException();
        }

        #endregion

        #endregion
    }
}
